package com.fresh.everynew.fp;

import io.vavr.CheckedFunction0;
import io.vavr.Function0;
import io.vavr.Function1;
import io.vavr.Function2;
import io.vavr.control.Option;
import io.vavr.control.Try;
import static io.vavr.Predicates.*;
import org.junit.jupiter.api.Test;


import static io.vavr.API.*;

public class vavr {
    @Test
    void name() {
        Function2<Integer, Integer, Integer> divide = (a, b) -> a / b;
        Function2<Integer, Integer, Option<Integer>> safeDivide = Function2.lift(divide);

        Function2<Integer, Integer, Integer> sum = Integer::sum;
        Function1<Integer, Integer> add2 = sum.curried().apply(2);

        CheckedFunction0<Integer> print = ()-> {System.out.println(1213);return 1213;};
        Function0<Integer> print1 = ()-> {System.out.println(123);return 123;};

        Integer result = Try.of(print)
                .recover(x -> Match(x).of(
                        Case($(instanceOf(NullPointerException.class)), print1),
                        Case($(instanceOf(IllegalArgumentException.class)), print1),
                        Case($(instanceOf(RuntimeException.class)), print1)
                ))
                .getOrElse(1);
    }
}
